home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
telecomm
/
zm16src.lzh
/
PHONE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-05-19
|
15KB
|
874 lines
/*
* Phone dialing Module (from XMDM)
*
*
* Jwahar Bammi
* usenet: mandrill!bammi@{decvax,sun}.UUCP
* csnet: bammi@mandrill.ces.CWRU.edu
* arpa: bammi@mandrill.ces.CWRU.edu
* CompuServe: 71515,155
*/
#include "config.h"
#ifndef STANDALONE
#ifdef PHONES
#include "zmdm.h"
typedef int WORD;
typedef long LONG;
#define sendchar(c) Bconout(1, c);
#define clear_screen() Bconws("\033H\033J")
#define inv() EscSeq('p')
#define normal() EscSeq('q')
#define mvto(r,c) EscSeq('Y');Bconout(2, r+040);Bconout(2, c+040)
#define ceol() EscSeq('K')
#define show_cursor() EscSeq('e')
#define hide_cursor() EscSeq('f')
typedef struct _dir { /* The Directory type */
char *name; /* Name (11 chars max) */
char *number; /* Phone # (24 chars max) */
WORD baud; /* baud Rate */
struct _dir *next; /* Ptr to next entry */
} *DIR;
/* External Variables */
extern WORD speed; /* Current Baud Rate */
extern WORD Baudrate;
extern BAUDS vbauds[];
WORD dchanged = 0; /* Has the directory been updated */
/* Globals */
static WORD ndir = 0; /* # of entries in phone directory */
static DIR directory = (DIR)NULL; /* The phone directory */
static DIR lastdir = (DIR)NULL; /* Pointer to last entry */
static char *dirfile = (char *)NULL; /* Name of file conatining directory */
static WORD rflag = 0; /* Read directory as yet?? */
/*
* Read the Telephone directory
* returns -2 on read error
* -1 if cancelled
* -3 if file not found
* n # of entries otherwise
*/
WORD readdir()
{
register char *filename;
register WORD handle;
register WORD nentries;
register DIR last;
register DIR present;
extern char *preadl();
extern char *malloc();
Bconws("Enter Filename of Phone Directory or <CR> to Cancel: ");
if((filename = preadl()) == (char *)NULL)
/* Cancelled */
return -1;
if((dirfile = malloc(strlen(filename)+1)) == (char *)NULL)
{
/* Out of memory */
Bconws("Out of Memory\r\n");
return 0;
}
strcpy(dirfile,filename);
if((handle = Fopen(filename,0)) < 0)
/* File does not exist */
return -3;
/* Read in the file */
if(Fread(handle, 2L, &ndir) != 2L)
{
Bconws("Error Reading ");
Bconws(filename);
Bconws("\r\n");
Fclose(handle);
return -2;
}
/* Read in the directory */
last = (DIR)NULL;
directory = (DIR)NULL;
lastdir = (DIR)NULL;
rflag = 1;
for(nentries = 0; nentries < ndir; nentries++)
{
/* Allocate an entry */
if((present = (DIR)malloc(sizeof(struct _dir))) == (DIR)NULL)
{
/* Out of memory */
Bconws("Out of Memory\r\n");
Fclose(handle);
return nentries;
}
if((present->name = malloc(12)) == (char *)NULL)
{
/* Out of memory */
Bconws("Out of Memory\r\n");
Fclose(handle);
return nentries;
}
if((present->number = malloc(25)) == (char *)NULL)
{
/* Out of memory */
Bconws("Out of Memory\r\n");
Fclose(handle);
return nentries;
}
present->next = (DIR)NULL;
/* Read in the entry */
if(Fread(handle,11L,present->name) != 11L)
{
Bconws("Error Reading ");
Bconws(filename);
Bconws("\r\n");
Fclose(handle);
rflag = 0;
freedir(directory);
return -2;
}
if(Fread(handle,24L,present->number) != 24L)
{
Bconws("Error Reading ");
Bconws(filename);
Bconws("\r\n");
Fclose(handle);
rflag = 0;
freedir(directory);
return -2;
}
if(Fread(handle,2L,&(present->baud)) != 2L)
{
Bconws("Error Reading ");
Bconws(filename);
Bconws("\r\n");
Fclose(handle);
rflag = 0;
freedir(directory);
return -2;
}
present->name[11] = '\0';
present->number[24] = '\0';
/* Link it on with the directory */
if(last == (DIR)NULL)
/* first entry */
directory = present;
else
last->next = present;
last = present;
}
lastdir = last;
return nentries;
}
/*
* Free space allocated to a phone directory
*
*/
freedir(dir)
register DIR dir;
{
register DIR next;
register DIR present;
for(present = dir; present != (DIR)NULL; present = next)
{
next = present->next;
free(present->name);
free(present->number);
free(present);
}
if(dirfile != (char *)NULL)
{
free(dirfile);
dirfile = (char *)NULL;
}
directory = (DIR)NULL;
lastdir = (DIR)NULL;
ndir = 0;
rflag = 0;
}
/*
* Write out the phone directory
* -returns 0 on success 1 otherwise
*
*/
WORD writedir()
{
register DIR dir;
register WORD fd;
if((rflag == 0) || (dirfile == (char *)NULL) || (dchanged == 0))
/* Nothing to Save */
return 0;
/* Create/Open file for write - overwrite if it exists */
if ((fd = Fcreate(dirfile,0)) < 0) /* Will fail if file is present */
{
/* Overwrite existing file */
if((fd = Fopen(dirfile,1)) < 0)
{
Bconws("Cannot Open ");
Bconws(dirfile);
Bconws("\r\n");
return 1;
}
}
if(Fwrite(fd,2L,&ndir) != 2L)
{
Bconws("Error Writing ");
Bconws(dirfile);
Bconws("\r\n");
Fclose(fd);
return 1;
}
for(dir = directory; dir != (DIR)NULL; dir = dir->next)
{
if(Fwrite(fd,11L,dir->name) != 11L)
{
Bconws("Error Writing ");
Bconws(dirfile);
Bconws("\r\n");
Fclose(fd);
return 1;
}
if(Fwrite(fd,24L,dir->number) != 24L)
{
Bconws("Error Writing ");
Bconws(dirfile);
Bconws("\r\n");
Fclose(fd);
return 1;
}
if(Fwrite(fd,2L,&(dir->baud)) != 2L)
{
Bconws("Error Writing ");
Bconws(dirfile);
Bconws("\r\n");
Fclose(fd);
return 1;
}
}
Fclose(fd);
return 0;
}
/*
* Show the phone directory
* return the entry # or -1 if cancelled
*
*/
WORD showdir()
{
register WORD first, last;
register WORD n;
register char *line;
extern WORD atoi();
extern char *preadl();
first = 0;
while(1)
{
again:
clear_screen();
mvto(0,19);
Bconws("Phone Directory: ");
Bconws(dirfile);
Bconws(" ");
printf("%d",ndir); fflush(stdout);
Bconws(" Entry(s)");
last = (ndir < (first + 44)) ? ndir : first + 44;
putdir(first,last);
/* mvto(25,0); */
Bconws("\r\n");
inv();
Bconws("Enter a # or <SPACE><RETURN> for Next Page or <RETURN> to Cancel:");
normal();
Bconout(2, ' ');
if((line = preadl()) == (char *)NULL)
{
return -1;
}
if(isdigit(*line))
{
if((n = atoi(line)) >= ndir)
{
Bconws("Invalid Number ");
hit_key();
goto again;
}
return n;
}
if(last == ndir)
first = 0;
else
first += 44;
}
}
/*
* Put up directory entries on the screen
*
*/
putdir(first,last)
register WORD first;
register WORD last;
{
register DIR dir;
register WORD row;
extern DIR nth();
/* Find the first entry */
dir = nth(first);
row = (first % 44) + 1;
hide_cursor();
inv();
for(; first < last; first++)
{
mvto(row,((first & 1)?41:0));
if(first < 10)
Bconout(2, ' ');
printf("%d",first); fflush(stdout);
Bconout(2, '|');
putstr(dir->name,11);
Bconout(2, '|');
putstr(dir->number,24);
dir = dir->next;
if(first & 1)
row++;
}
if(first & 1)
{
mvto(row,41);
Bconws(" | | ");
row++;
}
for(; row < 23; row++)
{
Bconws(" | | ");
mvto(row,41);
Bconws(" | | ");
}
normal();
show_cursor();
}
/*
* Put a string padding to len on screen
*/
putstr(s,l)
register char *s;
register WORD l;
{
register WORD pad;
Bconws(s);
if((pad = l - strlen(s)) <= 0)
return;
for(; pad > 0; pad--)
Bconout(2, ' ');
}
/*
* Return the nth entry in the phone directory
*
*/
DIR nth(n)
register WORD n;
{
register WORD i;
register DIR dir;
for(i = 0, dir = directory; (dir != (DIR)NULL) & (i < n);
i++, dir = dir->next)
/* Skip */;
return(dir);
}
/*
* Add a entry in the phonebook
*/
addi